import { _decorator, Component, Node, Label, Color } from 'cc';
const { ccclass, property } = _decorator;

// /** 调试 */
// window["Grid3"] = Grid3;

@ccclass('Grid3')
export default class Grid3 extends Component {
    @property(Node)
    normal: Node | null = null;
    @property(Node)
    empty: Node | null = null;
    @property(Node)
    right: Node | null = null;
    @property(Node)
    wrong: Node | null = null;
    @property(Node)
    answer: Node | null = null;
    @property(Label)
    Label_word: Label | null = null;
    @property(Node)
    select: Node | null = null;
    @property(Label)
    Label_index: Label | null = null;
    id: number = 0;
    x: number = 0;
    y: number = 0;
    index: number = -1;
    state = "empty";
    word_right: string;
    word_current: string;
    isActive: boolean = false;
    isFill: boolean = true;
    isRight: boolean = true;
    isComplete: boolean = false;
    //    // 初始化
    init(_id: number, _x: number, _y: number) {
        this.id = _id;
        this.x = _x;
        this.y = _y;

        // this.initWidthAndHeight(XXL_GameConfig.grid_size, XXL_GameConfig.grid_size);
        // this.initPosition();

        this.Refresh();
        this.AddListener();
        this.Select(false);

        this.Label_index.node.active = false;
    }
    AddListener() {
        this.node.on(Node.EventType.TOUCH_START, this.On_TOUCH_START, this);
    }
    On_TOUCH_START() { }
    Refresh() {
        this.normal.active = false;
        this.empty.active = false;
        this.right.active = false;
        this.wrong.active = false;
        this.answer.active = false;

        if (this.isActive === true) {
            if (this.isFill === true) {
                // this.wrong.active = true;
                if (this.word_right === this.word_current) {
                    this.right.active = true;

                    this.Label_word.color = new Color().fromHEX("#FFFFFF");
                } else {
                    this.wrong.active = true;
                }
            } else {
                this.empty.active = true;
            }
        } else {
            if (this.isComplete === true) {
                this.right.active = true;

                this.Label_word.color = new Color().fromHEX("#FFFFFF");
            } else {
                this.normal.active = true;
            }
        }
    }
    AllActiveFalse() {
        this.normal.active = false;
        this.empty.active = false;
        this.right.active = false;
        this.wrong.active = false;
        this.answer.active = false;
    }
    Right() {
        this.AllActiveFalse();
        this.right.active = true;
        this.Label_word.color = new Color().fromHEX("#FFFFFF");
    }
    Wrong() {
        this.AllActiveFalse();
        this.wrong.active = true;
    }
    Select(select: boolean): void {
        this.select.active = select;
    }
    //    // // 初始化单元宽高
    //    // initWidthAndHeight(cardWidth?: number, cardHeight?: number) {
    //    //     if (cardWidth) this.node.width = cardWidth;
    //    //     if (cardHeight) this.node.height = cardHeight;
    //    // }
    //    // // 初始化位置
    //    // initPosition() {
    //    //     const w = XXL_GameConfig.size;
    //    //     const h = XXL_GameConfig.size;
    //    //     let x = -XXL_GameConfig.W / 2 + w / 2 + w * this.x;
    //    //     let y = -XXL_GameConfig.H / 2 + h / 2 + h * this.y;
    //    //     this.node.position = v3(x, y, 0);
    //    // }
}